package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.beans.Beans;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wxc
 * @since 2021-07-08
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 用户登录
     *
     * @param member
     **/
    @Override
    public String login(UcenterMember member) {
        //获取
        String mobile = member.getMobile();
        String password = member.getPassword();
        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001, "登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobilerMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobilerMember == null){
            throw new GuliException(20001, "手机号不存在");
        }

        //判断密码
        //因为存储在数据库的密码肯定是加密的
        //把输入的密码进行加密，再和数据库的密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(mobilerMember.getPassword())){
            throw new GuliException(20001, "密码错误");
        }

        //判断用户是否被禁用
        if(mobilerMember.getIsDisabled()){
            throw new GuliException(20001, "登录失败");
        }

        //登录成功
        String token = JwtUtils.getJwtToken(mobilerMember.getId(), mobilerMember.getNickname());

        return token;
    }

    /**
     * 用户注册
     *
     * @param registerVo
     **/
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)){
            throw new GuliException(20001, "注册失败");
        }

        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new GuliException(20001, "验证码不正确");
        }

        //判断手机号是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new GuliException(20001, "已注册，请登录");
        }

        //数据添加数据中心
        UcenterMember member = new UcenterMember();
        //BeanUtils.copyProperties(registerVo, member);
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);    //用户不禁用
        member.setAvatar("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic4.zhimg.com%2Fv2-b6eae3250bb62fadb3d2527f466cf033_b.jpg&refer=http%3A%2F%2Fpic4.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1628344350&t=20f17e2b7084c37e558df94db70bba4f");

        baseMapper.insert(member);

    }

    /**
     * 根据openid进行查询
     *
     * @param openid
     **/
    @Override
    public UcenterMember getOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    /**
     * 查询某一天的注册人数
     *
     * @param day
     **/
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
